#ifndef _TOUCH_H
#define _TOUCH_H

#include <stdint.h>
#include <stdbool.h>
#include <FreeRTOS.h>
#include <task.h>
#include <Arduino.h>
#include "../shared_val.h"
#include "../colormap.h"
#include "../eeprom_manage.h"
#include "screen.h"
#include "FT6336U.h"

#define TOUCH_SDA 2
#define TOUCH_SCL 3

#define PinNotUsed 254
#define TOUCH_RST PinNotUsed

// FT6336U(int8_t sda, int8_t scl, uint8_t rst_n, uint8_t int_n);
FT6336U ft6336u(TOUCH_SDA, TOUCH_SCL, PinNotUsed, PinNotUsed);


void gesture_handler(uint8_t gesture){
    // static uint8_t last_gesture = Gestures::None;
    // if (last_gesture != gesture){
    //     if (gesture == Gestures::SlideUp){
    //         priv_cmap();
    //     }else if (gesture == Gestures::SlideDown){
    //         next_cmap();
    //     }
    //     eeprom_save_config();
    // }
    // last_gesture = gesture;
}

boolean tpIsConnected(){
    Wire1.beginTransmission((uint8_t)I2C_ADDR_FT6336U);
    uint8_t error = Wire1.endTransmission();
    Serial.printf("Touch with code %d\r\n", error);
    if (error != 0){return (false);}
    return (true);
} 

// 判断是否连接到`电容触摸传感器`
uint8_t touch_setup(void){
    ft6336u.begin();
    for(int i = 0; i < 10; i++)
    {
        if (tpIsConnected()){tp_status = TP_READY; Serial.println("Touch Ready"); return TP_READY;}
        Serial.println("Touch Not Found");
        delay(100);
    }
    tp_status = TP_NOTFOUND;
    return TP_NOTFOUND;
}

FT6336U_TouchPointType tp;
void touch_loop(){
   if (tp_status==TP_READY){
    static uint16_t x, y;
    static uint16_t start_x, start_y;
    static int diffx, diffy;
    static bool long_pushed = false;
    static unsigned long touch_pushed_start_time =  0;
    static bool touched = false;
    static int start_br = 100;
    tp = ft6336u.scan();
    static char tempString[128];
    sprintf(tempString, "FT6336U TD Count %d / TD1 (%d, %4d, %4d) / TD2 (%d, %4d, %4d)\n", tp.touch_count, tp.tp[0].status, tp.tp[0].x, tp.tp[0].y, tp.tp[1].status, tp.tp[1].x, tp.tp[1].y);
    Serial.print(tempString);
      // if( touch.tp.touching )
        //   {
        //      x= touch.tp.y;
        //      y = 240 - touch.tp.x;
        //      gesture_handler(touch.tp.gesture);
        //     //  Serial.println(touch.tp.gesture);
        //      if (touch_pushed_start_time == 0){touch_pushed_start_time = millis();}
        //      if (touched==false){start_x = x;  start_y = y; diffy=0; diffx=0;}  // 下降沿
        //      if (millis() - touch_pushed_start_time >= TOUCH_LONG_PUSH_T){
        //         long_pushed = true;
        //         diffx= start_x-x;
        //         diffy= start_y-y;
        //         set_brightness(start_br+diffy*5);
        //         }else{ // 短按的中间
                
        //         }
        //   }else{
        //      touch_pushed_start_time = millis();
        //      if (touched==true){  // 上升沿
        //         if (start_br == brightness){
        //            if (x < 224){flag_show_cursor=true; test_point[0] = x; test_point[1] = y;}
        //         }
        //         if (long_pushed==false){  // 短按时
        //            if (x < 224){flag_show_cursor=true; test_point[0] = x; test_point[1] = y;}
        //         }
        //         start_br = brightness;
        //         long_pushed = false;  // 上升沿将长按检测标识符进行复位
        //      }  
        //   }
        //   touched = touch.tp.touching;
   }
  delay(5);
}


void touch_task(void *ptr){
    for(;;){
        touch_loop();
    }
}


void touch_task_startup(){
  xTaskCreate(touch_task, "touch_task", 2048, NULL, 1, NULL);
}


#endif